package com.huobaibai.domain.entry;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name="t_user")

public class UserEO  implements UserDetails{
	private static final long serialVersionUID = -5753688683896096188L;

	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="t_user_seq")
	@SequenceGenerator(name="t_user_seq",sequenceName="seq_t_user", allocationSize=1)
	private Long id;
	
	@Column(name="user_id")
	private String userId;
	private String name;
	private Integer age;
	private String address;
	private String password;
	
	@ManyToMany(cascade= {CascadeType.REFRESH},fetch=FetchType.EAGER)
	private List<UserRoleEO> roles; 
	
	public UserEO() {
		super();
	}

	public UserEO( String userId,String name, Integer age, String address) {
		super();
		this.userId=userId;
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<UserRoleEO> getRoles() {
		return roles;
	}

	public void setRoles(List<UserRoleEO> roles) {
		this.roles = roles;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		List<GrantedAuthority> auths=new ArrayList<GrantedAuthority>();
		List<UserRoleEO> roles=this.getRoles();
		for(UserRoleEO userRole:roles) {
			auths.add(new SimpleGrantedAuthority(userRole.getName()));
		}
		
		System.out.println("use UserEO getAuthoritites");
		return auths;
	}
//
	@Override
	public String getUsername() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}
	
	
	
	

}
